package can;

import java.io.Serializable;



public class Element implements Serializable{
	public enum Nationality {NA, EU};
	public enum Year {y2010, y2011, y2012};
	public enum Company {GE, Oracle};
	public enum Doctype {MRI, DISCHARGE};
	
	//set defaults
	String name;
	Nationality nationality;
	Year year;
	Company company;
	Doctype doctype;
	
	String url;
	
	//default constructor
	public Element() {
		name = "default";
		nationality = Nationality.NA;
		year = Year.y2012;
		company = Company.GE;
		doctype = Doctype.MRI;
	}
	
	public Element(String name, Nationality nationality, Year year, Company company, Doctype doctype) {
		this.name = name;
		this.nationality = nationality;
		this.year = year;
		this.company = company;
		this.doctype = doctype;
	}
	
	//decoder constructor
	//I am unsure of how we should allow for changing of this string encoding/decoding with changes in the database,
	//and changes in the nationality, year, etc tags.
	public Element(String encoded) {
		this.name = encoded.substring(encoded.indexOf(":") + 1,encoded.indexOf(","));
	
		encoded = encoded.substring(encoded.indexOf(",")+1);
		
		String nat = encoded.substring(encoded.indexOf(":") + 1,encoded.indexOf(","));
		if (nat.equals("NA")) 
			this.nationality = Nationality.NA;
		else if (nat.equals("EU"))
			this.nationality = Nationality.EU;
		encoded = encoded.substring(encoded.indexOf(",")+1);
		
		String year = encoded.substring(encoded.indexOf(":") + 1, encoded.indexOf(","));
		if (year.equals("y2010"))
			this.year = Year.y2010;
		else if (year.equals("y2011"))
			this.year = Year.y2011;
		else if (year.equals("y2011"))
			this.year = Year.y2012;
		encoded = encoded.substring(encoded.indexOf(",")+1);
		
		String company = encoded.substring(encoded.indexOf(":") + 1, encoded.indexOf(","));
		if (company.equals("GE"))
			this.company = Company.GE;
		else if (company.equals("Oracle"))
			this.company = Company.Oracle;
		encoded = encoded.substring(encoded.indexOf(",")+1);
		
		String doctype = encoded.substring(encoded.indexOf(":") + 1, encoded.indexOf(","));
		if (doctype.equals("MRI"))
			this.doctype = Doctype.MRI;
		else if (doctype.equals("Discharge"))
			this.doctype = Doctype.DISCHARGE;
		encoded = encoded.substring(encoded.indexOf(",")+1);
		
		this.url = encoded.substring(encoded.indexOf(":") + 1, encoded.indexOf(","));
		
		System.out.println("Decoded:" + this);
	}
	
	public boolean satisfiesOption(Option o) {
		if ((""+nationality).equals(o.getName())){
			if (o.getIsActive() == false)
				return false;
		}
		else if ((""+year).equals(o.getName())){
			if (o.getIsActive() == false)
			return false;
		}
		
		else if ((""+company).equals(o.getName())){
			if (o.getIsActive() == false)
			return false;
		}
		
		else if ((""+doctype).equals(o.getName())){
			if (o.getIsActive() == false)
			return false;
		}
		else {
			System.err.println("SATISFIESOPTION UNRECOGNISED OPTION");
		}
		
		return true;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public Nationality getNationality() {
		return nationality;
	}

	public void setNationality(Nationality nationality) {
		this.nationality = nationality;
	}

	public Year getYear() {
		return year;
	}

	public void setYear(Year year) {
		this.year = year;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public Doctype getDoctype() {
		return doctype;
	}

	public void setDoctype(Doctype doctype) {
		this.doctype = doctype;
	}

	public String toString() {
		return "[Name:" + name + 
				",Nationality:" + nationality +
				",Year:" + year + 
				",Company:" + company +
				",Doctype:" + doctype + 
				",Url:" + url + ",]"; 
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
}
